package com.huike.business.service.impl;


import com.huike.business.domain.TbBusiness;
import com.huike.business.domain.TbBusinessTrackRecord;
import com.huike.business.domain.vo.BusinessTrackVo;
import com.huike.business.domain.vo.TbBusinessTrackRecordVo;
import com.huike.business.mapper.TbBusinessMapper;
import com.huike.business.mapper.TbBusinessTrackRecordMapper;
import com.huike.business.service.ITbBusinessTrackRecordService;
import com.huike.common.utils.bean.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 商机跟进记录Service业务层处理
 *
 * @author wgl
 * @date 2021-04-28
 */
@Service
public class TbBusinessTrackRecordServiceImpl implements ITbBusinessTrackRecordService {
    @Autowired
    private TbBusinessTrackRecordMapper businessTrackRecordMapper;
    @Autowired
    private TbBusinessMapper tbBusinessMapper;

    @Override
    public void add(BusinessTrackVo businessTrackVo) {
        TbBusinessTrackRecord tbBusinessTrackRecord = new TbBusinessTrackRecord();
        BeanUtils.copyProperties(businessTrackVo, tbBusinessTrackRecord);
        businessTrackRecordMapper.insert(tbBusinessTrackRecord);
        TbBusiness tbBusiness = new TbBusiness();
        BeanUtils.copyProperties(businessTrackVo, tbBusiness);
        tbBusiness.setId(businessTrackVo.getBusinessId());
        tbBusinessMapper.updateTbBusiness(tbBusiness);
    }

    @Override
    public List<TbBusinessTrackRecordVo> getBusinessById(Long id) {
        List<TbBusinessTrackRecord> tbBusinessTrackRecords = businessTrackRecordMapper.selectAll(id);
        List<TbBusinessTrackRecordVo> tbs = tbBusinessTrackRecords.stream().map(item -> {
            TbBusinessTrackRecordVo tbBusinessTrackRecordVo = new TbBusinessTrackRecordVo();
            BeanUtils.copyProperties(item, tbBusinessTrackRecordVo);
            if (item.getKeys() != null){
                String[] split = item.getKeys().split(",");
                List<String> strings = Arrays.asList(split);
                tbBusinessTrackRecordVo.setKeys(strings);
            }
            return tbBusinessTrackRecordVo;
        }).collect(Collectors.toList());
        return tbs;
    }
}
